home *** CD-ROM | disk | FTP | other *** search
MarxMenu script | 1994-07-14 | 17.4 KB | 774 lines |
- Comment
- ==========================================================
-
- Computer Tyme IniTyme * Copyright 1993-94 by Marc Perkel
- All Rights Reserved
-
- Computer Tyme * 411 North Sherman, Suite 300 * Springfield Mo. 65802
- (800) 548-5353 Sales * (417) 866-1222 Voice * (417) 866-1665 Data/Fax
-
- IniTyme is a Windows *.INI file manipulator. It is designed to assist the
- network administrator who has to maintain INI files for many users.
-
- USAGE: INITYME ChangeFile IniFile
-
- Example:
- INITYME CHANGE.INI SYSTEM.INI
-
- ==========================================================
- EndComment
-
- ;------ Create Variables
-
- var
- Orig
- UOrig
- KOrig
- Changes
- UChanges
- NameIndex
- BlockStartIndex
- BlockEndIndex
- CurrentSection
- ThisSection
- SectionStart
- SectionEnd
- SubFind
- SubReplace
- SubFindG
- SubReplaceG
- GroupSection
- GroupMode
- Sections
- InsertPos
- Log
- Logging
- LogFileName
- LogGroup
- BackName
- NoBackup
- RestoreMode
- TestMode
-
- var
- IfArray
- Stack
- GotoList
- IfLine
- GroupList
- ParenLevel
-
- Main
-
- ;======================= P R O C E D U R E S ===========================
-
- Comment
- ================================================
-
- The file is read into the array Orig. It is then processed and a
- parallel array UOrig is created. The lines in UOrig are upper case with
- extra spaces and comments removed and are more suitable for searching
- and comparing.
-
- NameIndex contains a list of group headings. BlockStartIndex contains a list
- of line numbers that groups start. BlockEndIndex contains a list og line
- numbers where groups end. That way when I search I know what range of
- line numbers to start and end at.
-
- ================================================
- EndComment
-
-
- Procedure IndexFile
- var LastTextLine
- HashLevel = 2
- UOrig = Orig
- Loop UOrig
- LoopVal = CleanIniLine(LoopVal)
- if LoopVal StartsWith '['
-
- ;-- Process Header
-
- ThisSection = LoopVal
- GroupSection = ThisSection = '[GROUPS]'
- CurrentSection = CurrentSection + 1
-
- AppendArray(NameIndex,LoopVal)
- AppendArray(BlockStartIndex,succ(LoopIndex))
- if CurrentSection > 1
- AppendArray(BlockEndIndex,LastTextLine)
- endif
- else
- if LoopVal > ''
- LastTextLine = LoopIndex
- KOrig[LoopIndex] = KeyString(LoopVal)
- endif
- endif
- EndLoop
- Sections = CurrentSection
- CurrentSection = 0
- if Sections > 0
- AppendArray(BlockEndIndex,LastTextLine)
- endif
- EndProc
-
-
- Procedure KeyString (St)
- var KeyWord
- KeyWord = LeftOfEqual(St)
- if Hash(KeyWord + ThisSection)
- Return St
- endif
- GroupMode = False
- if GroupSection
- if St StartsWith 'GROUP'
- GroupMode = True
- KeyWord = 'GROUP=' + FilePart(RightOfEqual(St))
- endif
- endif
- Return KeyWord
- EndProc
-
-
- ;----- When adding or deleting the BlockStartIndex must be updated
-
- Procedure AdjustLineIndex (Line, Adj)
- Loop BlockStartIndex
- if Line <= LoopVal
- LoopVal = LoopVal + Adj
- endif
- EndLoop
- Loop BlockEndIndex
- if Line <= LoopVal
- LoopVal = LoopVal + Adj
- endif
- EndLoop
- EndProc
-
-
- Procedure LogEvent ($St)
- if not Logging then Return
- if LogGroup <> ThisSection
- if LogGroup <> ''
- AppendArray(Log,'')
- endif
- AppendArray(Log,ThisSection)
- LogGroup = ThisSection
- endif
- AppendArray(Log,St)
- EndProc
-
-
- Procedure DelLine (Line)
- if Line = 0 then Return
- LogEvent ' Deleted: ' Orig[Line]
- Delete(Orig,Line,1)
- Delete(UOrig,Line,1)
- Delete(KOrig,Line,1)
- AdjustLineIndex(Line,-1)
- EndProc
-
-
- Procedure InsertLine (St,Line)
- ArrayInsert(Orig,Line,1)
- ArrayInsert(UOrig,Line,1)
- ArrayInsert(KOrig,Line,1)
-
- Orig[Line] = St
- UOrig[Line] = CleanIniLine(St)
- KOrig[Line] = KeYString(UOrig[Line])
- EndProc
-
-
- ;----- Change a line, 0 for line number adds line
-
- Procedure ChangeOrAddLine (St,Line)
- var Original
- if Line = 0
-
- ;- new line
-
- if CurrentSection > 0
-
- ;- Determine Insert Position
-
- if InsertPos = 0
- Line = BlockEndIndex[CurrentSection] + 1
- else
- Line = InsertPos
- InsertPos = 0
- endif
-
- if GroupMode
- St = 'Group' + Str(NextGroup) + '=' + RightOfEqual(St)
- endif
-
- InsertLine(St,Line)
-
- AdjustLineIndex(Line - 1,1)
- LogEvent ' Added: ' St
- endif
- else
-
- ;- change line
-
- Original = Orig[Line]
- if InsertPos > 0
-
- ;- move the line
-
- Delete(Orig,Line,1)
- Delete(UOrig,Line,1)
- Delete(KOrig,Line,1)
- Line = InsertPos
-
- InsertLine(St,Line)
- InsertPos = 0
- endif
-
- if GroupMode
- St = LeftOfEqual(Orig[Line]) + RightOfEqual(St)
- endif
-
- if St <> Original
- LogEvent ' Changed: ' Original ' to ' St
- Orig[Line] = St
- UOrig[Line] = CleanIniLine(St)
- KOrig[Line] = KeyString(UOrig[Line])
- endif
- endif
- EndProc
-
-
- Procedure AddSection (St)
- var Lines Proc
- Proc = CleanIniLine(St)
-
- ;- Add a blank line
-
- AppendArray(Orig,'')
- AppendArray(UOrig,'')
- AppendArray(KOrig,'')
-
- ;- Add CurrentSection header
-
- AppendArray(Orig,St)
- AppendArray(UOrig,Proc)
- AppendArray(KOrig,Proc)
-
- ;- Update Indexes
-
- Lines = NumberOfElements(UOrig)
- AppendArray(NameIndex,Proc)
- AppendArray(BlockStartIndex,Lines)
- AppendArray(BlockEndIndex,Lines)
-
- ;- Make new CurrentSection the current CurrentSection
-
- CurrentSection = NumberOfElements(NameIndex)
-
- if Logging
- LogGroup = UpperCase(St)
- AppendArray(Log,'')
- LogEvent LogGroup ' -*- Section Added'
- endif
-
- EndProc
-
-
- Procedure DelSection (St)
- var S E D
- FindSection(St)
- if CurrentSection = 0 then Return
-
- S = BlockStartIndex[CurrentSection] - 1
- E = BlockEndIndex[CurrentSection]
- D = E - S + 1
-
- delete(Orig,S,D)
- delete(UOrig,S,D)
- delete(KOrig,S,D)
-
- delete(NameIndex,CurrentSection,1)
- delete(BlockStartIndex,CurrentSection,1)
- delete(BlockEndIndex,CurrentSection,1)
- AdjustLineIndex(S,0 - D)
- CurrentSection = 0
-
- if Logging
- LogGroup = ''
- AppendArray(Log,'')
- LogEvent UpperCase(St) ' -*- Section Deleted'
- AppendArray(Log,'')
- endif
-
- EndProc
-
-
- Procedure FindSection (St)
- CurrentSection = PosInList(St,NameIndex)
- if CurrentSection = 0
- ThisSection = ''
- GroupSection = False
- SectionStart = 0
- SectionEnd = 0
- else
- ThisSection = St
- SectionStart = BlockStartIndex[CurrentSection]
- SectionEnd = BlockEndIndex[CurrentSection]
- GroupSection = ThisSection = '[GROUPS]'
- endif
- EndProc
-
-
- Procedure FindLine (St)
- Return PosInList(KeyString(St),KOrig,SectionStart,SectionEnd)
- EndProc
-
-
- ;----- Replaces text in block from the list of substitute text.
-
- Procedure ApplySubstBlock (SubF,SubR,S,E)
- var P St
- if NumberOfElements(SubF) = 0 then Return
- while S <= E
- Loop SubF
-
- repeat
- P = Pos(LoopVal,UOrig[S])
- if P > 0
- St = Orig[S]
- delete(St,P,Length(LoopVal))
- insert(SubR[LoopIndex],St,P)
- ChangeOrAddLine(St,S)
- endif
- until P = 0
-
- EndLoop
- S = S + 1
- endwhile
- dispose(SubFind)
- dispose(SubReplace)
- EndProc
-
-
- Procedure ApplySubst
- if CurrentSection = 0 then Return
- ApplySubstBlock(SubFind,SubReplace,BlockStartIndex[CurrentSection],BlockEndIndex[CurrentSection])
- ApplySubstBlock(SubFindG,SubReplaceG,BlockStartIndex[CurrentSection],BlockEndIndex[CurrentSection])
- EndProc
-
-
- Procedure NextGroup
- var X S E St Match
- X = 1
- repeat
- S = BlockStartIndex[CurrentSection]
- E = BlockEndIndex[CurrentSection]
- Match = False
- while (S <= E) and not Match
- St = 'GROUP' + Str(X)
- Match = pos(St,UOrig[S]) > 0
- S = S + 1
- endwhile
- if not Match then Return X
- X = X + 1
- until False
- EndProc
-
-
- Procedure RemoveExtraBlankLines
- var Tmp
- Trim(Orig)
- Tmp = Orig
- dispose Orig
- Loop Tmp
- if (LoopVal > '') or (Tmp[LoopIndex + 1] > '')
- AppendArray(Orig,LoopVal)
- endif
- EndLoop
- EndProc
-
-
- Procedure AddItem (Line)
- var St List New UNew NewLine Next UNext
- St = UOrig[Line]
- New = RightOfEqual(Changes[LoopIndex])
- UNew = RightOfEqual(UChanges[LoopIndex])
- St = RightOfEqual(St)
- NewLine = Orig[Line]
- while St > ''
- AppendArray(List,NextWord(St))
- endwhile
- while UNew > ''
- Next = NextWord(New);
- UNext = NextWord(UNew);
- if PosInList(UNext,List) = 0
- NewLine = NewLine + ' ' + Next
- endif
- endwhile
- ChangeOrAddLine(NewLine,Line)
- EndProc
-
-
- Procedure DelItem (Line)
- var St List New UNew NewLine Next UNext P
- St = UOrig[Line]
- UNew = RightOfEqual(UChanges[LoopIndex])
- St = St + ' '
- NewLine = Orig[Line]
- while UNew > ''
- UNext = NextWord(UNew);
- P = pos(UNext + ' ',St)
- if P > 0
- delete(St,P,length(UNext) + 1)
- delete(NewLine,P,length(UNext) + 1)
- endif
- endwhile
- TrimTrail(NewLine)
- ChangeOrAddLine(NewLine,Line)
- EndProc
-
-
- Procedure ChangeFile
- var St New Line Tmp FirstWord RestOfLine X Y
- Loop Changes
- New = LoopVal
- St = UChanges[LoopIndex]
- if St > ''
-
- RestOfLine = St
- FirstWord = NextWord(RestOfLine)
-
- if Hash(FirstWord)
- St = RestOfLine
- Tmp = NextWord(New)
-
- if CurrentSection = 0
- if FirstWord = 'TESTMODE'
- TestMode
-
- elseif FirstWord = 'LOG'
- LogFileName = RestOfLine
- if LogFileName = ''
- LogFileName = ForceExtension(BackName,'LOG')
- endif
- Logging
-
- elseif FirstWord = 'NOBACKUP'
- NoBackup = True
-
- endif
- endif
-
- if FirstWord = 'DEL'
- if RestOfLine StartsWith '['
- ApplySubst
- DelSection(RestOfLine)
- else
- DelLine(FindLine(RestOfLine))
- endif
-
- elseif FirstWord = 'ADD'
- Line = FindLine(St)
- if Line = 0
- ChangeOrAddLine(New,0)
- endif
-
- elseif FirstWord = 'CHANGE'
- Line = FindLine(St)
- if Line > 0
- ChangeOrAddLine(New,Line)
- endif
-
- elseif FirstWord = 'ADDVALUE'
- Line = FindLine(St)
- if Line > 0
- X = Value(RightOfEqual(New)) + Value(RightOfEqual(UOrig[Line]))
- Tmp = LeftOfEqual(New)
- ChangeOrAddLine(Tmp + Str(X),Line)
- endif
-
- elseif FirstWord = 'ADDITEM'
- Line = FindLine(St)
- if Line > 0
- AddItem(Line,St)
- else
- ChangeOrAddLine(New,0)
- endif
-
- elseif FirstWord = 'DELITEM'
- Line = FindLine(St)
- if Line > 0
- DelItem(Line,St)
- endif
-
- elseif FirstWord = 'SUBST'
- if CurrentSection = 0
- AppendArray(SubFindG,CleanIniLine(NextWord(St)))
- AppendArray(SubReplaceG,NextWord(St))
- else
- AppendArray(SubFind,CleanIniLine(NextWord(St)))
- AppendArray(SubReplace,NextWord(St))
- endif
-
- elseif FirstWord = 'BEFORE'
- InsertPos = FindLine(St)
-
- elseif FirstWord = 'AFTER'
- InsertPos = succ(FindLine(St))
-
- elseif FirstWord = 'FIRST'
- InsertPos = BlockStartIndex[CurrentSection]
-
- endif
-
- else
-
- ;- Process Sections
-
- if St StartsWith '['
-
- ;- New Group
-
- ApplySubst
- FindSection(St)
- if CurrentSection = 0
- AddSection(LoopVal)
- endif
-
- else
-
- ;- Process Lines
-
- ChangeOrAddLine(New,Findline(St))
-
- endif
-
- endif
- endif
- IfLine = IfArray[LoopIndex]
- Interpret
- if NumberOfElements Stack <> 0
- Error ('Too Many Parameters!',LoopIndex)
- endif
- EndLoop
- ApplySubst
- ApplySubstBlock(SubFindG,SubReplaceG,1,NumberOfElements(Orig))
- EndProc
-
-
- Procedure Help
- var Help HelpFile
- HelpFile = ExistOnPath 'INITYME.HLP'
- if HelpFile = ''
- Writeln
- Writeln 'IniTyme * Copyright 1993 by Marc Perkel'
-
- Include 'ADDRESS.INC'
-
- Writeln 'Missing file INITYME.HLP'
- else
- BoxHeader ' Viewing ' + HelpFile + ' '
- DrawBox 1 1 ScreenWidth ScreenHeight
- ViewTextFile(HelpFile)
- endif
- ExitMenu
- EndProc
-
-
- Procedure Beg
- var Jessica
- BoxHeader ' * Shameless Beg Screen * '
- DrawBox 10 8 61 7
- Writeln
- WriteCenter '* IniTyme Evaluation Copy *'
- Writeln
- WriteCenter 'Please remember to register this software.'
- Writeln
- if Timer and 1 = 0
- Jessica = Now - TimeOf('1-17-80') / SecondsInDay / 365 + 2
- WriteCenter 'I have a ' Jessica ' year old daughter who wants to go shopping.'
- else
- WriteCenter "I'd sure hate to have to find a real job."
- endif
- Wait 600
- EraseTopWindow
- ClearKbdBuffer
- EndProc
-
-
- Procedure Error (St,Line)
- Writeln
- if Line > 0
- Writeln 'IniTyme Error in line ' Line
- Writeln St
- else
- Writeln 'IniTyme Error: ' St
- endif
- ExitMenu
- EndProc
-
-
- ;----- INITYME.INC has the conditional logic.
-
- Include 'INITYME.INC'
-
-
- Procedure AddIniExtension (Name)
- Name = UpperCase(Name)
- if pos('.',Name) > 0 then Return Name
- Return ForceExtension(Name,'INI')
- EndProc
-
-
- Procedure Setup
- BoxBorderColor Green Blue
- BoxInsideColor White Blue
- BoxHeaderColor Yellow Mag
- ;Beg
- RestoreMode = UpperCase(ParamStr(2)) = 'RESTORE'
- if not RestoreMode
- if (ParamStr(2) = '') or not ExistFile(AddIniExtension(ParamStr(2)))
- Help
- endif
- endif
- StandardIO
- if NovConnection > 0
- NovReadGroups(NovLoginName,GroupList)
- SortArray(GroupList)
- endif
-
- ;-- Duplicates List - Add your own duplicates here.
-
- HashLevel = 3
- Hash('DEVICE=[386ENH]') = True
-
- ReadTextFile(AddIniExtension(ParamStr(2)),Changes)
- Loop Changes
- Trim(LoopVal)
- LoopVal = EnvExpandString(LoopVal)
- EndLoop
- UChanges = Changes
- Loop UChanges
- LoopVal = CleanIniLine(LoopVal)
-
- if LoopVal StartsWith 'IF '
- IfArray[LoopIndex] = LoopVal
- LoopVal = ''
- Changes[LoopIndex] = ''
- PushStack(LoopIndex)
-
- elseif LoopVal = 'ENDIF'
- LoopVal = ''
- Changes[LoopIndex] = ''
- GotoList[PopStack] = LoopIndex
-
- elseif LoopVal = 'ELSE'
- LoopVal = ''
- Changes[LoopIndex] = ''
- IfArray[LoopIndex] = 'GOTO'
- GotoList[PopStack] = LoopIndex
- PushStack(LoopIndex)
-
- endif
-
- EndLoop
-
- ;-- if you forgot an Endif then the stack contains values.
-
- if NumberOfElements Stack <> 0
- Error ('MisMatched Conditionals!',0)
- endif
-
- HashLevel = 4
- Hash('ADD') = True
- Hash('ADDITEM') = True
- Hash('ADDVALUE') = True
- Hash('AFTER') = True
- Hash('BEFORE') = True
- Hash('CHANGE') = True
- Hash('DEL') = True
- Hash('DELITEM') = True
- Hash('FIRST') = True
- Hash('LOG') = True
- Hash('NOBACKUP') = True
- Hash('SUBST') = True
- Hash('TESTMODE') = True
-
- SetupLibWords
- EndProc
-
-
- Procedure ResetVariables
- dispose(NameIndex)
- dispose(BlockStartIndex)
- dispose(BlockEndIndex)
- dispose(SubFind)
- dispose(SubReplace)
- dispose(SubFindG)
- dispose(SubReplaceG)
- dispose(Log)
- LogGroup = ''
- CurrentSection = 0
- EndProc
-
-
- Procedure ProcessFile (Name)
- var FileList MultiFile
- Name = AddIniExtension(Name)
- BackName = ForceExtension(Name,'BNI')
- ReadTextFile(Name,Orig)
- if NumberOfElements Orig > 0
- if ExistFile(Orig[1])
- MultiFile = True
- FileList = Orig
- Loop FileList
- ProcessFile(LoopVal)
- EndLoop
- endif
- endif
- if not MultiFile
- if RestoreMode
- if ExistFile(BackName)
- Writeln 'Restoring ' Name
- DelFile(Name)
- FileRename(BackName,Name)
- else
- Writeln BackName ' not Found!'
- endif
- else
- ResetVariables
- Write 'Converting: ' Name ' '
- IndexFile
- ChangeFile
- RemoveExtraBlankLines
-
- if TestMode
- WriteTextFile(BackName,Orig)
- else
- DelFile(BackName)
- FileRename(Name,BackName)
- WriteTextFile(Name,Orig)
- endif
-
- if NoBackup
- DelFile(BackName)
- endif
-
- if Logging
- Trim(Log)
- WriteTextFile(LogFileName,Log)
- endif
-
- Writeln 'Done!'
- endif
- endif
- EndProc
-
-
- Procedure Main
- Setup
- ProcessFile(ParamStr(3))
- EndProc
-